/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wf.ui;

import bc.dsl.SwingDSL;
import bc.swing.pfrm.BaseParamModel;
import bc.swing.pfrm.FieldParamModel.ChangeListener;
import bc.swing.pfrm.Model;
import bc.swing.pfrm.Page;
import bc.swing.pfrm.ano.Action;
import bc.swing.pfrm.ano.OnEvent;
import bc.swing.pfrm.ano.PageDef;
import bc.swing.pfrm.ano.Param;
import bc.swing.pfrm.viewtypes.ParamType;
import bc.utils.CalendarUtils;
import java.util.Date;
import java.util.List;
import javax.swing.ImageIcon;
import wf.model.Schedule;
import wf.ui.cmps.CalendarView;
import wf.ui.cmps.SchedulingModel;
import wf.ui.layouts.SchedulingLayout;
import wf.units.ModificationUnit;
import wf.units.DatabaseUnit;

/**
 *
 * @author bennyl
 */
@PageDef(layout = SchedulingLayout.class, name = "Scheduler", icon = "cc-clock")
public class SchedueManager extends Model {

    public static final String CALENDAR_PARAM = "Calendar";
    public static final String SELECTED_SCHEDULES_PARAM = "Selected Schedules";
    SchedulingModel smodel;

    public SchedueManager() {
        smodel = new SchedulingModel();
        Page.get(smodel).getParam(SchedulingModel.SCHEDULES_PARAM).addChangeListener(new ChangeListener() {

            @Override
            public void onChange(BaseParamModel source, Object newValue, Object deltaHint) {
                syncToView(SELECTED_SCHEDULES_PARAM);
            }
        });
    }

    @Param(name = CALENDAR_PARAM, customView = CalendarView.class, role = SchedulingLayout.CALENDAR_ROLE)
    public SchedulingModel getCalendar() {
        return smodel;
    }

    @OnEvent(name = DatabaseUnit.DATABASE_CHANGED_EVENT, extract = {DatabaseUnit.DBCE_DATA, DatabaseUnit.DBCE_CHANGE_TYPE})
    public void onDatabaseChangeEvent(Object changed, DatabaseUnit.ChangeType changeType) {
        if (changed instanceof Schedule) {
            Schedule sched = (Schedule) changed;
            if (CalendarUtils.isTheSameMonth(sched.getDayTime(), smodel.getSelectedDate().getTime())) {
                smodel.setSelectedDate(smodel.getSelectedDate()); //will cause refresh...
            }
        }
    }

    @Param(name = SELECTED_SCHEDULES_PARAM, type = ParamType.LIST, role = SchedulingLayout.SELECTED_SCHEDULING_ROLE)
    public List<Schedule> getSelectedSchedules() {
        return smodel.getViewedSchedules();
    }

    @Action(name = "Add Schedule", forParam = SELECTED_SCHEDULES_PARAM, icon = "calendar-insert")
    void handleAddSchedule() {
        ModificationUnit.UNIT.invokeScheduleCreation(smodel.getSelectedDate().getTime());
    }

    @Action(name = "Delete Selected", forParam = SELECTED_SCHEDULES_PARAM, icon = "delete22")
    void handleDeleteSchedule() {
        Schedule selected = (Schedule) Page.get(this).getParam(SELECTED_SCHEDULES_PARAM).getSelectedItem();
        if (selected != null) {
            DatabaseUnit.UNIT.deleteItem(selected);
            smodel.updateViewedSchedules();
            syncToView();
        }
    }

    @Override
    public ImageIcon provideParamValueIcon(String param, Object value) {
        if (param.equals(SELECTED_SCHEDULES_PARAM)) {
            return SwingDSL.resIcon("calendar-event");
        }
        return super.provideParamValueIcon(param, value);
    }
}
